package rsexplorer;

import rsexplorer.debugger.*;

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.swing.JButton;

import javax.swing.JFrame;

import rsexplorer.utils.ClassResolver;

public class Main extends JFrame implements AppletStub, ActionListener {
    private static final String mainClass = "client";

    private static final long serialVersionUID = 7290678744249645100L;
    private String prefix = "";
    private String referer = "http://www.google.com/#hl=en&source=hp&q=runescape&btnG=Google+Search&meta=&aq=f&oq=runescape&fp=324c8a90db775e43";
    private final HashMap<String, String> parameters = new HashMap<String, String>();
    private Applet mainClient;

    private ClassResolver resolver;
    
    public Main(String prefix) {
        super("RuneScape");
        try {
            System.out.println("static g lol".compareTo("static g lo"));
            this.prefix = prefix;

            resolver = new ClassResolver(new URL[] {new URL("file:runescape-614.jar")});

            loadParams();
            loadClient();

            buildGUI();//*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String readPage(URL url) throws IOException, InterruptedException {
        String s = readPage(url, referer);
        referer = url.toExternalForm();
        return s;
    }

    private String readPage(URL url, String referer) throws IOException, InterruptedException {
        URLConnection uc = url.openConnection();
        uc.addRequestProperty("Accept",
                "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        uc.addRequestProperty("Accept-Charset",
                "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
        uc.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
        uc.addRequestProperty("Connection", "keep-alive");
        uc.addRequestProperty("Host", "www.runescape.com[url=http://][/url][url=http://][/url]");
        uc.addRequestProperty("Keep-Alive", "300");
        if (referer != null) {
            uc.addRequestProperty("Referer", referer);
        }
        uc.addRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6");
        DataInputStream di = new DataInputStream(uc.getInputStream());
        byte[] buffer = new byte[uc.getContentLength()];
        di.readFully(buffer);
        di.close();
        Thread.sleep(250 + (int) Math.random() * 500);
        return new String(buffer);
    }

    private final void loadParams() throws MalformedURLException, IOException, InterruptedException {
        String html = readPage(new URL("http://" + prefix + ".runescape.com/a2,m0,j0,o0"));
        try {
            Pattern regex = Pattern.compile("<param name=([^\\s]+)\\s+value=\"{0,1}(.*?)\"{0,1}>",
                    Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
            Matcher regexMatcher = regex.matcher(html);
            while (regexMatcher.find()) {
                if (!parameters.containsKey(regexMatcher.group(1))) {
                    parameters.put(regexMatcher.group(1), regexMatcher.group(2));
                }
            }
        } catch (PatternSyntaxException ex) {
            ex.printStackTrace();
        }
    }

    private final void loadClient() throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
        try {
            mainClient = (Applet) resolver.loadClass(mainClass).newInstance();
            mainClient.transferFocus();
            mainClient.setStub(this);
            mainClient.init();
            mainClient.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void buildGUI() {
        getContentPane().add(mainClient, BorderLayout.CENTER);

        JButton debugField = new JButton("Debug");
        debugField.addActionListener(this);

        getContentPane().add(debugField, BorderLayout.SOUTH);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000, 600);

        setVisible(true);   
    }

    @Override
    public void appletResize(int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public AppletContext getAppletContext() {
        throw new UnsupportedOperationException();
    }

    @Override
    public URL getCodeBase() {
        try {
            return new URL("http://" + prefix + ".runescape.com/");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public URL getDocumentBase() {
        return getCodeBase();
    }

    @Override
    public String getParameter(String name) {
        return parameters.get(name);
    }

    public void actionPerformed(ActionEvent e) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Debugger(resolver).setVisible(true);
            }
        });
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            new Main("world106");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
